﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Husb.Framework
{
    public class BusinessManagerBase<TEntity, TDao> : IBusinessManager<TEntity>
        where TEntity : EntityBase, new()
        where TDao : Husb.Framework.DataAccess.DataAccessBase<TEntity>, new()
    {
        #region 属性

        //private static TDao dao = null;
        //public static TDao Dao
        //{
        //    get
        //    {
        //        if (adapter == null)
        //        {
        //            adapter = new TDataAccessAdapter();
        //        }
        //        return adapter;
        //    }
        //}
        private volatile static TDao _dao = null;
        private static readonly object lockHelper = new object();
        public static TDao dao
        {
            get
            {
                if (_dao == null)
                {
                    lock (lockHelper)
                    {
                        if (_dao == null)
                            _dao = new TDao();
                    }
                }
                return _dao;
            }
        }

        #endregion

        #region IBusinessManager<TEntity> 成员

        IList<TEntity> IBusinessManager<TEntity>.FindAll(bool enableCache)
        {
            throw new NotImplementedException();
        }

        IList<TEntity> IBusinessManager<TEntity>.Find(string where, string orderBy, bool enableCache)
        {
            throw new NotImplementedException();
        }

        TEntity IBusinessManager<TEntity>.GetById(Guid id, bool enableCache)
        {
            throw new NotImplementedException();
        }

        int IBusinessManager<TEntity>.GetCount(string where, bool hideDeleted)
        {
            throw new NotImplementedException();
        }

        IList<TEntity> IBusinessManager<TEntity>.GetPaged(string where, bool hideDeleted, string sortExpression, int maximumRows, int startRowIndex)
        {
            throw new NotImplementedException();
        }

        bool IBusinessManager<TEntity>.Insert(TEntity entity, bool removeCache, string[] cacheKey)
        {
            throw new NotImplementedException();
        }

        bool IBusinessManager<TEntity>.Update(TEntity entity, bool removeCache, string[] cacheKey)
        {
            throw new NotImplementedException();
        }

        bool IBusinessManager<TEntity>.Delete(Guid id, bool removeCache, string[] cacheKey)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region 静态成员

        #region 获取集合
        public static IList<TEntity> FindAll(bool enableCache)
        {
            return dao.FindAll(enableCache);
        }

        public static IList<TEntity> Find(string where, string orderBy, bool enableCache)
        {
            return dao.Find(where, orderBy, enableCache);
        }

        #endregion

        #region 获取单个对象
        public static TEntity GetById(Guid id, bool enableCache)
        {
            return dao.GetById(id, enableCache);
        }

        #endregion

        #region 获取关联对象

        public static IList<TEntity> GetByMasterId(Guid masterId)
        {
            return dao.GetByMasterId(masterId);
        }

        #endregion

        #region 分页
        public static int GetCount(string where, bool hideDeleted)
        {
            return dao.GetCount(where, hideDeleted);
        }

        public static IList<TEntity> GetPaged(string where, bool hideDeleted, string sortExpression, int maximumRows, int startRowIndex)
        {
            return dao.GetPaged(where, hideDeleted, sortExpression, maximumRows, startRowIndex);
        }
        #endregion

        #region 更新

        public static bool Insert(TEntity entity, bool removeCache, string[] cacheKey)
        {
            return dao.Insert(entity, removeCache, cacheKey);
        }

        public static bool Update(TEntity entity, bool removeCache, string[] cacheKey)
        {
            return dao.Update(entity, removeCache, cacheKey);
        }
        #endregion

        #region 删除
        public static bool Delete(Guid id, bool removeCache, string[] cacheKey)
        {
            return dao.Delete(id, removeCache, cacheKey);
        }
        #endregion

        #endregion

        #region 重载
        public static IList<TEntity> FindAll()
        {
            return dao.FindAll(false);
        }

        public static IList<TEntity> Find(string where, string orderBy)
        {
            return dao.Find(where, orderBy, false);
        }

        public static IList<TEntity> Find(string where)
        {
            return dao.Find(where, null, false);
        }

        public static TEntity GetById(Guid id)
        {
            return dao.GetById(id, false);
        }

        public static int GetCount(string where)
        {
            return dao.GetCount(where, true);
        }

        public static IList<TEntity> GetPaged(string where, string sortExpression, int maximumRows, int startRowIndex)
        {
            return dao.GetPaged(where, true, sortExpression, maximumRows, startRowIndex);
        }

        public static bool Insert(TEntity entity)
        {
            return dao.Insert(entity, true, null);
        }

        public static bool Update(TEntity entity)
        {
            return dao.Update(entity, true, null);
        }

        public static bool Delete(Guid id)
        {
            return dao.Delete(id, true, null);
        }

        #endregion

        #region 重写

        

        #endregion
    }
}
